/* eslint-disable no-alert */

import loadingCpt from '../components/public/loading.vue'
import Vue from 'vue'

// eslint-disable-next-line no-alert, quotes, semi
function loading ({ target = document.body, duration = 0, text = '', mask = false, opacity = 0 }) {
  let MJLoading = Vue.extend(loadingCpt)
  if (target instanceof Vue) target = target.$el
  let mjLoading = new MJLoading({
    data () {
      return {
        parent: target
      }
    }
  }).$mount()

  if (!target) {
    document.body.appendChild(mjLoading.$el)
  } else {
    target.appendChild(mjLoading.$el)
  }
  mjLoading.$on('finished', () => {
    mjLoading = null
  })
  mjLoading.show({ duration, text, mask, opacity })
  return mjLoading
}
export default loading